<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/7/11
 * Time: 11:04
 */

namespace app\command\company\data_change;

use think\console\Command;
use think\console\Input;
use think\console\Output;

class CompanyOrderStorage extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('data:change:1')->setDescription('库存表中添加经销商ID');
    }


    protected function execute(Input $input, Output $output)
    {
        $output->writeln('库存表中添加经销商ID开始');
        $model = new \app\mini\model\CompanyOrderStorage();
        $model->startTrans();
        try {
            $model->select()->each(function ($item) {
               $supplier_id = (int)$item->companyOrderDetail()->value('supplier_id');
               $item->supplier_id = $supplier_id;
               $item->save();
            });
            $model->commit();
            $output->writeln('库存表中添加经销商ID成功');
        } catch (\Exception $e) {
            $model->rollback();
            $output->writeln('库存表中添加经销商ID失败' . $e->getMessage() . $e->getLine() . $e->getFile());
        }
    }
}